#include "Administrator.h"
#include "logistics.h"
#include "cashier.h"

//销售菜单
void cash_menu(void)
{
    for(;;)
    {
        system("clear");
        puts("*******销售模块*******");
        puts("|     1、销售        |");
        puts("|     2、VIP登录     |");
        puts("|     3、返回上一级  |");

        switch (get_cmd('1','3'))
        {
            case '1':trade();break;
            case '2':vip_login();break;
            case '3':return;
        }
    }
}

//游客销售
void trade()
{
    float all_p = 0;
    char phone[12];
    printf("请输入付款账户:");
    get_str(phone,sizeof(phone));
    if(11 != strlen(phone))
  	{
    	print_sec("请输入11位手机号！",SECOND);
    	return;
  	}
    
    for(;;)
    {   
        char id[20];
        printf("请输入商品id:");
        get_str(id,sizeof(id));
        bool flag=false;
        if(0==strcmp(id,"0000"))
        {
            break;
        }
        for(int i=0; i<cnt; i++)
	    {
            if(0 == strcmp(goods[i].id,id))
            {
                flag=true;
                float number;
                printf("请输入数量：");
	            scanf("%f",&number);
	            int num = number;
                //判断非法输入
	            if(0>num || num!=number || num>goods[i].num)
	            {
		            print_sec("数量输入有误!",SECOND);
		            break;
	            }
	            goods[i].num -= num;
                all_p += goods[i].price*num;
                printf("商品ID：%s\t 名称：%s\t 价格：%f\t ",
			            goods[i].id,goods[i].name,goods[i].price);
                printf("本次消费%f元！\n",all_p);
                anykey_continue();
            }
        }
        if(flag)
            continue;
        print_sec("查询不到该商品！",SECOND);
        
    }
        if(0==all_p)
        {
            print_sec("本次没有购买！",SECOND);
            return;
        }
        printf("从账户%s扣款：%f元！\n",phone,all_p);
        account.money[account.cnt_msg] = all_p;
        account.cnt_msg++;
        printf("付款成功!\n");
        anykey_continue();
}

//VIP登录
void vip_login(void)
{
    char phone[12];
    char passwd[7];
    printf("请输入您的手机号：");
    get_str(phone,sizeof(phone));
    if(11 != strlen(phone))
  	{
    	print_sec("请输入11位手机号！",SECOND);
    	return;
  	}
    for (int i = 0;i<cnt_vip;i++)
    {
        if (!strcmp(phone,vip[i].phone))
        {
            printf("请输入您的密码(6位)：");
            get_passwd(passwd,sizeof(passwd));
            if(6 != strlen(passwd))
  			{
    			print_sec("请输入6位密码！",SECOND);
    			return;
  			}
            if (!strcmp(passwd,vip[i].vip_passwd))
            {
                vip_menu(i);
                return;
            }
            else 
            {
                print_sec("密码错误，请重试！",SECOND);
			    return;
            }
        }
    }
    print_sec("查无此人!",SECOND);
	return;
}

//VIP菜单
void vip_menu(int i)
{
    for(;;)
    {
        system("clear");

        puts("******VIP销售模块*****");
        puts("|     1、销售        |");
        puts("|     2、查看积分    |");
        puts("|     3、修改密码    |");
        puts("|     4、返回上一级  |");

        switch(get_cmd('1','4'))
        {
        case '1':vip_trade(i);break;
        case '2':vip_point(i);break;
        case '3':change_vip(i);break;
        case '4':return;
        }
    }
}

//VIP销售
void vip_trade(int i)
{
    char id[20];
    float all_p = 0;
    for(;;)
    {   
        printf("请输入商品id:");
        get_str(id,sizeof(id));
        bool flag=false;
        if(0==strcmp(id,"0000"))
        {
            break;
        }
        for(int i=0; i<cnt; i++)
	    {
            
            if(0 == strcmp(goods[i].id,id))
            {
                flag=true;
                float number;
                printf("请输入数量：");
	            scanf("%f",&number);
	            int num = number;
                //判断非法输入
	            if(0>num || num!=number || num>goods[i].num)
	            {
		            print_sec("数量输入有误!",SECOND);
		            break;
	            }
	            goods[i].num -= num;
                all_p += goods[i].price*num;
                printf("商品ID：%s\t 名称：%s\t 价格：%f\t ",
			            goods[i].id,goods[i].name,goods[i].price);
                printf("本次消费%f元！\n",all_p);
                anykey_continue();
            }
        }
        if(flag)
            continue;
        print_sec("查询不到该商品！",SECOND);
    }
    if(0==all_p)
    {
        print_sec("本次没有购买！",SECOND);
        return;
    }
    printf("使用VIP积分抵扣：");
    //vip积分抵扣
    if(yes_or_no())
    {
        //判断积分扣款后是否小于0.01
        if(all_p-0.01*vip[i].points <= 0.01)
        {
            printf("从账户%s扣款：0.01元！\n",vip[i].phone);
            vip[i].points += 1 - 99*all_p;  
            account.money[account.cnt_msg] += 0.01;
            account.cnt_msg++;
        }
        //正常积分抵扣
        else
        {
            printf("从账户%s扣款：%.2f元！\n",vip[i].phone,all_p-0.01*vip[i].points);
            vip[i].points=0;
            vip[i].points += (all_p-0.01*vip[i].points);  
            account.money[account.cnt_msg] += (all_p-0.01*vip[i].points);
            account.cnt_msg++;
        }
    }
    //不用积分，正常扣款
    else
    {
        printf("从账户%s扣款：%.2f元！\n",vip[i].phone,all_p);
        vip[i].points += all_p;  
        account.money[account.cnt_msg] += all_p;
        account.cnt_msg++;
    }
        printf("付款成功!\n");
        anykey_continue();
}

//VIP查看积分
void vip_point(int i)
{
    printf("您的积分为：%.2f\n",vip[i].points);
    anykey_continue();
    return;
}

//VIP修改密码
void change_vip(int i)
{
    char passwd[7];
    printf("请输入原密码(6位)：");
    get_str(passwd,sizeof(passwd));
    if (!strcmp(vip[i].vip_passwd,passwd))
    {
        printf("请输入新密码(6位)\n");
		char new_passwd[7];
		get_str(new_passwd,sizeof(new_passwd));
		strcpy(vip[i].vip_passwd,new_passwd);
		print_sec("重置成功!",SECOND);
		return;
    }
}



